<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 常用函数
 * @version $Id: util_helper.php 3761 2013-05-24 02:10:24Z trjcn_pss $
 */


/**
 * 创建目录
 * @param $path
 * @return unknown_type
 */
function mkpath($path)
{
	!is_dir($path) && mkdir($path, 0777, TRUE);
}


/**
 * 
 * @return unknown_type
 */
function header_move($url)
{
	header( "HTTP/1.1 301 Moved Permanently");
	header('location:'.$url);
	exit;
}

function is_splider()
{
	if (empty($_SERVER['HTTP_USER_AGENT']))
	{
		return FALSE;
	}

	$searchEngineBot = array(
	'spider',
	'google',
	'msnbot',
	'yodaobot',
	'youdaobot',
	'yahoo',
	'crawler',
	);

	$spider = strtolower($_SERVER['HTTP_USER_AGENT']);
	foreach ($searchEngineBot as $item)
	{
		if (strpos($spider, $item)!== false)
		{
			return TRUE;
		}
	}
	return FALSE;
}



/**
 * 中文截取
 * @param $str
 * @param $length
 * @param $suffix
 * @return unknown_type
 */
function sub_str($str, $length, $suffix=true)
{
	$str = strip_tags(htmlspecialchars_decode($str));
	if (strlen($str) <= $length )
	{
		return $str;
	}
	else
	{
		$i = 0;
		while ($i < $length)
		{
			$StringTMP = substr($str,$i,1);
			if ( ord($StringTMP) >=224 )
			{
				$StringTMP = substr($str,$i,3);
				$i = $i + 3;
			}
			elseif( ord($StringTMP) >=192 )
			{
				$StringTMP = substr($str,$i,2);
				$i = $i + 2;
			}
			else
			{
				$i = $i + 1;
			}
			$StringLast[] = $StringTMP;
		}
		$StringLast = implode("",$StringLast);
		if($suffix)
		{
			$StringLast .= $end;
		}
		return $StringLast;
	}
}

/**
 * 部分字符串
 * */
function hiddenStr($str,$type,$length=3,$suffix="***"){
	if($str){
		$len	=	mb_strlen($str,"utf-8");
		if($type == 1){// 字**符
			$i    = ceil($len/4);
			$j    = $len-($i*2);
			$k    = $i+$j;
			return mb_substr($str,0,$i,"utf-8").$suffix.mb_substr($str,$k,$len,"utf-8");
		}else if($type ==2){// **字符
			return $suffix.mb_substr($str,3,$len,"utf-8");
		}
		else{//字符***
			return mb_substr($str,0,$len-3,"utf-8")."***";
		}
	}
}



function is_email($str)
{

	return (preg_match('/^[-a-zA-Z0-9_\.]+\@([0-9A-Za-z][0-9A-Za-z-]+\.)+[A-Za-z]{2,5}$/',$str) ? true : false );

}

function is_mobile($str,$is_more=FALSE)
{
	if($is_more)
	{
		$_str=explode(",",$str);
		$count  =       count($_str);
		for($i=0;$i<$count;$i++)
		{
			if(!is_mobile($_str[$i]))return false;
		}
		return true;
	}
	else
	{
		return (preg_match('/1[3|5|7|8|][0-9]{9}/',$str) ? true : false );
	}
}


function is_tel($str, $is_more=FALSE)
{
	if($is_more)
	{
		$_str=explode(",",$str);
		$count = count($_str);
		for($i=0;$i<$count;$i++){
			if(!is_tel($_str[$i]))return false;
		}
		return true;
	}
	else
	{
		return (preg_match('/^\(?0?(10|2[0-57-9]|[3-9]\d{2})\)?-?\d{7,8}(-\d{0,50})?$/',$str) ? true : false );
	}

}


function alert($msg, $url=FALSE)
{
	echo '<script>alert("'.$msg.'");</script>';
	if (is_bool($url))
	{
		echo $url ? '<script>history.back();</script>' : '';
	}
	elseif($url)
	{
		echo '<script>location.href="'.$url.'";</script>';
	}
	exit;
}

/**
 * 错误提示方法
 * @param $msg 将错误信息数组
 * @param $url 提示错误后跳转的地址
*/
function alert_err($err=array(), $url=FALSE)
{
	$qry_string = $_SERVER['QUERY_STRING'];
	if (is_array($err)) {
		switch (ENVIRONMENT) {
			case 'development':
				$msg = '';
				foreach ($err as $v) {
					$msg .= ($v);
				}
				$msg = strip_tags($msg);
				break;
			default:
				$msg = $err[0];
				break;
		}
	} else {
		$msg = $err;
	}
	log_write('error',$msg);
	echo '<script>alert("'.$msg.'");</script>';
	if (is_bool($url))
	{
		echo $url ? '<script>history.back();</script>' : '';
	}
	elseif($url)
	{
		echo '<script>location.href="'.$url.'";</script>';
	}
	exit;
}
/**
* 是否站内
*/
function is_from_inner()
{
	if (!isset($_SERVER['HTTP_REFERER']) || !$_SERVER['HTTP_REFERER']) return false;
	return preg_match('~http:\/\/(\w+)'.TRJ_DOMAIN.'~','http://'.$_SERVER['HTTP_REFERER']);
}

function utf_to_gbk($val)
{
	return iconv('utf-8', 'gbk', $val);
}

function gbk_to_utf($val)
{
	return iconv('gbk', 'utf-8', $val);
}

function check_pwd($pw) 
{
    if (strlen($pw) < 6) return 1;
    $c = 0;
    if (preg_match('/[a-z]+/', $pw)) $c++;
    if (preg_match('/[0-9]+/', $pw)) $c++;
    if (preg_match('/[^a-zA-Z0-9]+/', $pw)) $c++;
    if ($c < 2) {
        $s = "0123456789abcdefghigklmnopqrstuvwxyz";

        $arr = preg_split('//', strtolower($pw), -1, PREG_SPLIT_NO_EMPTY);
        $idx = strpos($s, $arr[0]);
        if ($idx > -1) {
            $arr2 = preg_split('//', $s, -1, PREG_SPLIT_NO_EMPTY);
            $len = count($arr);
            $tlen= count($arr2);
            for ($i = 0; $i < $len; $i++) {
                if ($idx + 1 >= $tlen || $arr[$i] != $arr2[$idx + $i]) {
                    $c++;
                    break;
                }
            }
        }
    }
    if ($c > 1) {
        if (strlen($pw) >= 8) $c++;
        if (preg_match('/[^a-zA-Z0-9]+/', $pw)) $c++;
    }

    if ($c > 4) $c = 4;

    return $c;
};

function search_filter($str)
{
	$str = str_replace(array('"',"'"), '', $str);
	if(strlen($str) > 100)$str = mb_substr($str, 0, 100, 'utf-8');
	return $str;
}

//取出br标记
function strip($text, $tags = array())
{
	$text = html_entity_decode($text);
    return $text;
}

if ( ! function_exists('log_write'))
{
	function log_write($log_type, $message, $log_aliyun=false)
	{
		if (defined('NEED_LOG_ALIYUN')) $log_aliyun = NEED_LOG_ALIYUN;
		$method = debug_backtrace();
		$ci_obj =& get_instance();
		$time_str=date('Y-m-d H:i:s');	//日志时间，本地存储和阿里云存储，要统一
		$local_ext=' ';		//初始化本地log附加信息
		//获取附加的log信息
		$log_info['L_time']=  $time_str;
		$log_info['L_type']=  $log_type;
		$log_info['L_class']=  $ci_obj->router->class;
		$log_info['L_method']=  $method[1]['function'];
		$sess_key_arr=array('L_order_no','L_deal_no','L_recharge_no','L_refund_batch');		//可能会有的session
		foreach ($sess_key_arr as $sesskey) {
			if( isset($_SESSION[$sesskey]) ){
				$log_info[$sesskey] = $_SESSION[$sesskey];
				$local_ext.='#'.$sesskey.':'.$log_info[$sesskey].'; ';		//本地log附加信息
			}
		}
		if( isset($ci_obj->loginID) && $ci_obj->loginID > 0 ){
			$log_info['L_uid'] = $ci_obj->loginID;		//登录的用户id
			$local_ext.='#L_uid:'.$log_info['L_uid'].'; ';		//本地log附加信息
		}
		$log_info['L_sessid']=  session_id();	//会话id
		$local_ext.='#L_sessid:'.$log_info['L_sessid'].'; ';		//本地log附加信息
		$log_info['L_message']=  $message;

		$message = '['.$ci_obj->router->class.'-'.$method[1]['function'].']'.$local_ext.$message;		//拼装本地log信息
		$ci_obj->load->library('Log_qb');
		$log_obj=new Log_qb();
		$log_obj->write_log('ERROR', $message, FALSE, $log_type,$time_str);		//写入本地log文件

		//测试阿里云的内容
//		$json_str=json_encode($log_info);
//		$file_name='yunlog/'.date('Y-m-d His',strtotime($time_str)).'.txt';
//		file_put_contents($file_name, $json_str."\n\n",FILE_APPEND );
		if ($log_aliyun) {
			//使用接口，将日志写入阿里云日志服务
			$ci_obj->config->load('/aliyun_sls');
			$cfg_sls = $ci_obj->config->item('aliyun_sls');
			$url = $cfg_sls['url'];
			$data['project_name'] = $cfg_sls['project_name'];
			if( ENVIRONMENT == 'production' ){
				$data['logs_name'] = 'payment-log-collect';
			}
			else{
				$data['logs_name'] = 'payment-log-collect-test';
			}
			$data['post_data'] = '['.json_encode($log_info).']';
			https_curlPost($url,$data);
		}
	}
}

// ------------------------------------------------------------------------

/* End of file util_helper.php */
/* Location: ./application/frontend/heleprs/util_helper.php */
